package cc.telecomdigital.tdstock.activity.groups.more;

import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.RelativeLayout;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.ProgressDialog;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.CommonDefn.ModeType;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.Framework.UniqueItem.NetworkChangeListener.CONNECT_TYPE;
import cc.telecomdigital.tdstock.Framework.UniqueItem.NetworkChangeListener.INetworkChangeListener;
import cc.telecomdigital.tdstock.Tools.ActivityGroupController;
import cc.telecomdigital.tdstock.activity.BaseActivity;
import cc.telecomdigital.tdstock.activity.groups.MoreGroup;
import cc.telecomdigital.tdstock.activity.groups.monitor.util.HttpHelper;
import cc.telecomdigital.tdstock.activity.groups.more.push.util.Data;

/**
 * 
 * Project：tdstock3
 * Package：cc.telecomdigital.tdstock.activity.groups.more.push.setting
 * Class：MessageSettingActivity Description： Creater：hunter
 * [chengpanggang@126.com//hunter@mango.cc] CreateTime：Date：Oct 31, 2011
 * Time：10:28:36 AM Modifier ：hunter [chengpanggang@126.com//hunter@mango.cc]
 * Modify time： Date：Oct 31, 2011 Time：10:28:36 AM Modify Remarks：
 * 
 * @version
 * 
 */
public class PriceAlertActivity extends/* Activity_Framework*/  BaseActivity implements
		OnClickListener,  INetworkChangeListener{

	private Button breturn;
	private WebView webview;
	public static  final String TAG=PriceAlertActivity.class.getSimpleName();
	public static boolean flag = false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if (((ITDLApplication) this.getApplicationContext()).getModeType() == ModeType.DELARY)
		{
			setContentView(R.layout.more_message_delay);
			Button _toolBtnReturn = (Button) findViewById(R.id.more_message_returnbtn);
			_toolBtnReturn.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					Intent intent = new Intent(getApplicationContext(),
							MoreActivity.class);
					intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
					SwitchForwardActivity(MoreActivity.class, intent);
				}
			});

		}else{
		
		
				this.setContentView(R.layout.more_price_alert);
				breturn=(Button)findViewById(R.id.more_price_alert_return);
				breturn.setOnClickListener(this);
				ViewGroup container = (ViewGroup)findViewById(R.id.price_alert_container);
				webview = new WebView(this.getParent() == null ? this : this.getParent());
				webview.getSettings().setJavaScriptEnabled(true);
				webview.setScrollbarFadingEnabled(true);
				webview.setScrollContainer(true);
				webview.setVerticalScrollBarEnabled(false);
				webview.setHorizontalScrollBarEnabled(false);
				webview.setOnFocusChangeListener(new OnFocusChangeListener() {
					
					@Override
					public void onFocusChange(View v, boolean hasFocus) {
						// TODO Auto-generated method stub
						HideKeyBoard();
					}
				});
				webview.setWebViewClient(new WebViewClient(){
		
					@Override
					public boolean shouldOverrideUrlLoading(WebView view, String url) {
						// TODO Auto-generated method stub
						webview.loadUrl(url);
						return true;
					}
		
		
					
				});
				
				webview.requestFocus();
				RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FILL_PARENT,  RelativeLayout.LayoutParams.FILL_PARENT);
				 param.addRule(RelativeLayout.BELOW ,R.id._price_alert_setting_ctn_top);	
				container.addView(webview, param);
		}
	}
		


	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		TDStockLog.d(TAG, "close current activity and forwarding to the previous");

		Intent intent = new Intent(getApplicationContext(), MoreActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
		SwitchForwardActivity(MoreActivity.class, intent);
	}

	public void onResume(){
		super.onResume();
		if(((ITDLApplication)this.getApplicationContext()).getModeType()==ModeType.DELARY)
		{
			
		}else{
		            new PriceAlert(this.getParent() == null ? this : this.getParent() ).execute();
		}
	}
  class PriceAlert extends AsyncTask<Void, Void, Void>{
	  private ProgressDialog pd;
	  private Context context;
	  private String  priceAlertURL;
	  public PriceAlert(Context context){
		  this.context=context;
	  }
	/* (non-Javadoc)
	 * @see android.os.AsyncTask#doInBackground(Params[])
	 */
	@Override
	protected Void doInBackground(Void... params) {
		priceAlertURL= HttpHelper.isServerAvaiable(Data.getPriceAlertURL(context), context, 1);
		//TDStockLog.d(TAG, "token对比："+PriceAlertActivity.this.getSessionToken().equals(Data.token)+"");
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		super.onPostExecute(result);
		
		if(null!=priceAlertURL&&!"".equals(priceAlertURL)) {
			webview.post( new Runnable(){
					public void run(){
						webview.loadUrl(priceAlertURL);
					}
			});
			
		}
		else {
			
			webview.post( new Runnable(){
				public void run(){
					String  content = PriceAlertActivity.this.getResources().getString(R.string.service_not_available);
					String summary = "<html><body><div>"
						+ content.replaceAll("[\n]", "<br>").replaceAll(
								"&gt;", ">").replaceAll("&lt;", "<")
								.replaceAll("&amp;", "&")
						+ "</div></body></html>";
					webview.loadDataWithBaseURL(null, summary, "text/html","utf-8", null);
				}
		});
			
			
		}
		
		ProgressDialog.dismissAll();
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
		pd=new ProgressDialog(context,context.getString(R.string.downing));
		pd.show();
	}
	
 }
  
  
  @Override
	public ActivityGroupController GetActivityGroupController()
	{
		return MoreGroup.GetActivityGroupHostControl();
	}
  
  
	@Override
	public void NetworkChange_Response(CONNECT_TYPE iConnect,
			NetworkInfo iNetInfo) {

		//super.NetworkChange_Response(iConnect, iNetInfo);
		if(iNetInfo.isAvailable() || iNetInfo.isConnected()) {
			if(((ITDLApplication)this.getApplicationContext()).getModeType()==ModeType.DELARY){
				
			}else{
			       new PriceAlert(this.getParent() == null ? this : this.getParent() ).execute();
			}
		}
		
	}
	
	private void HideKeyBoard()
	{
		View viewObj = getCurrentFocus();
		InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		if (mgr.isActive() && (viewObj != null))
			mgr.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
					0);
	}
	
  
  
}
